import pygame
import sys
import random
import math
import time

# 初始化pygame
pygame.init()

# 设置画布大小
size = width, height = 500, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Two Entities Game")

# 设置颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
COLOR1 = (255, 0, 0)  # 实体1的颜色
COLOR2 = (0, 0, 255)  # 实体2的颜色


# 实体类
class Entity:
    def __init__(self, color):
        self.color = color
        self.radius = 5
        self.position = self.random_position()

    def random_position(self):
        return [random.randint(0, width), random.randint(0, height)]

    def draw(self):
        pygame.draw.circle(screen, self.color, self.position, self.radius)


# 计算两点之间的欧氏距离
def calculate_distance(pos1, pos2):
    return math.sqrt((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2)


# 游戏主循环
def main():
    entity1 = Entity(COLOR1)
    entity2 = Entity(COLOR2)
    num_rounds = 10
    round_duration = 10  # 每局持续10秒
    clock = pygame.time.Clock()

    for round_num in range(num_rounds):
        start_time = time.time()
        entity1.position = entity1.random_position()
        entity2.position = entity2.random_position()

        while calculate_distance(entity1.position, entity2.position) < 10:
            entity2.position = entity2.random_position()

        while time.time() - start_time < round_duration:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            screen.fill(WHITE)
            entity1.draw()
            entity2.draw()

            distance = calculate_distance(entity1.position, entity2.position)
            print(f"Round {round_num + 1}: Distance = {distance:.2f}")

            if distance < 100:
                pygame.draw.line(screen, BLACK, entity1.position, entity2.position, 1)
            else:
                break

            pygame.display.update()
            clock.tick(60)

    pygame.quit()


if __name__ == "__main__":
    main()
